
#include "flash_op.h"

uint16_t	g_u16_flash_dat[DAT_SIZE_MAX_PARM]	= {0u};

void read_prg_dat(void)//读FLASH存储数据，读取200个u16
{
    uint16_t u16lv_i = 0u;
    for(u16lv_i = 0u; u16lv_i < DAT_SIZE_MAX_PARM; u16lv_i ++)  //180
    {
        g_u16_flash_dat[u16lv_i] = *(uint16_t*)(BASE_ADDR_USER_PRG + (uint16_t)(u16lv_i << 1)); /* 从指定页的地址开始读 */
    }
}



uint8_t write_prg_dat(void) //写FLASH的存储数据
{
    uint16_t u16lv_i		= 0u;
    uint8_t u8lv_rtn	= 0u;
    FLASH_Status em_flash_sta = FLASH_COMPLETE;
    
    FLASH_Unlock();  /* 解锁flash编程擦除控制器 */
    FLASH_ClearFlag(FLASH_FLAG_BSY|FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPERR); /* 清除标志位*/
    /* FLASH_FLAG_BSY            Flash忙标志位 */
    /* FLASH_FLAG_EOP            FLASH操作结束标志位 */
    /* FLASH_FLAG_PGERR            FLASH编写错误标志位 */
    /* FLASH_FLAG_WRPERR       FLASH页面写保护错误标志*/
    em_flash_sta = FLASH_ErasePage(BASE_ADDR_USER_PRG);     /* 擦除指定页31of32 */
    if(em_flash_sta == FLASH_COMPLETE){
        for(u16lv_i = 0u; u16lv_i < DAT_SIZE_MAX_PARM; u16lv_i ++)
        {
        	em_flash_sta = FLASH_ProgramHalfWord(BASE_ADDR_USER_PRG + (uint16_t)(u16lv_i << 1u), g_u16_flash_dat[u16lv_i]); /*从指定页的addr地址开始写*/
        }
    }else{
    	;
    }
    if(em_flash_sta == FLASH_COMPLETE){
		u8lv_rtn = 0u;
    }else{
		u8lv_rtn = 1u;
	}
    FLASH_ClearFlag(FLASH_FLAG_BSY|FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPERR);/*清除标志位*/
    FLASH_Lock();    /*锁定FLSAH编程擦除控制器*/
	return u8lv_rtn;
}
